﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Windows.Data.Xml.Dom;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Data;

namespace BoolanElement.UI.Table
{
    public class BooBorderStyle
    {
        public const int Solid=0;
        public const int Dotted=1;
        public int Style;
        public Color BorderColor;
        public int BorderThickness;
        public BooBorderStyle()
        {
            Style=Solid;
            BorderColor = TableConfiguration.DefaultStrokeBrush.Color;
            BorderThickness=1;
        }
        public BooBorderStyle Clone()
        {
            return (BooBorderStyle)this.MemberwiseClone();
        }
    }
    public class BooTableStyleUnit : DependencyObject
    {

        public const int BackgroundColorChanged = 0;
        public const int BorderStyleChanged = 1;
        public const int FontChanged = 2;
        public const int BorderThicknessChanged = 3;
        public const int BorderColorChanged = 4;

        public const int FontColorChanged = 5;
        public const int FontSizeChanged = 6;
        public const int FontFamilyChanged = 7;
        public const int IsBoldChanged = 8;
        public const int IsItalicChanged = 9;
        public const int IsUnderlineChanged = 10;
        public const int IsStrikeoutChanged = 11;
        public const int AlignmentChanged = 12;


        public Color BackgroundColor;
        public Color FontColor;
        public int FontSize;
        public String FontFamily;
        public bool IsBold;
        public bool IsItalic;
        public bool IsUnderline;
        public bool IsStrikeout;
        public VerticalAlignment VerticalAlignment;
        public TextAlignment TextAlignment;
        public double Width;
        public double Height;
        public BooTableStyleUnit()
        {
            BackgroundColor = TableConfiguration.DefaultFillBrush.Color;
            FontColor = Color.FromArgb(255, 0, 0, 0);
            FontSize = 14;
            FontFamily = "微软雅黑";
            IsBold = false;
            IsItalic = false;
            IsUnderline = false;
            IsStrikeout = false;
            this.TextAlignment = TextAlignment.Center;
            this.VerticalAlignment = VerticalAlignment.Center;
            Width = 50;
            Height = 40;
        }

        public BooTableStyleUnit(int row,int column)
        {
            BackgroundColor = TableConfiguration.GetTableBrush(row,column).Color;
            
            FontColor = Color.FromArgb(255, 0, 0, 0);
            FontSize = 12;
            FontFamily = "微软雅黑";
            IsBold = false;
            IsItalic = false;
            IsUnderline = false;
            IsStrikeout = false;
            this.TextAlignment = TextAlignment.Center;
            this.VerticalAlignment = VerticalAlignment.Center;
            Width = 50;
            Height = 40;
        }

        public BooTableStyleUnit(BooTableStyleUnit unit)
        {

            BackgroundColor = unit.BackgroundColor;
            FontColor = unit.FontColor;
            FontSize = unit.FontSize;
            FontFamily = unit.FontFamily;
            IsBold = unit.IsBold;
            IsItalic = unit.IsItalic;
            IsUnderline = unit.IsUnderline;
            IsStrikeout = unit.IsStrikeout;
            this.TextAlignment = unit.TextAlignment;
            this.VerticalAlignment = unit.VerticalAlignment;
            Width = unit.Width;
            Height = unit.Height;
        }

        public void Load(BooTableStyleUnit unit)
        {
            BackgroundColor = unit.BackgroundColor;
            FontColor = unit.FontColor;
            FontSize = unit.FontSize;
            FontFamily = unit.FontFamily;
            IsBold = unit.IsBold;
            IsItalic = unit.IsItalic;
            IsUnderline = unit.IsUnderline;
            IsStrikeout = unit.IsStrikeout;
            this.TextAlignment = unit.TextAlignment;
            this.VerticalAlignment = unit.VerticalAlignment;
            Width = unit.Width;
            Height = unit.Height;
        }

        public XElement CreateElement(int row, int column)
        {
            XElement styleUnit = new XElement("StyleUnit");
            styleUnit.SetAttributeValue("Row", row.ToString());
            styleUnit.SetAttributeValue("Column", column.ToString());
            styleUnit.SetAttributeValue("Width", Width.ToString());
            styleUnit.SetAttributeValue("Height", Height.ToString());
            XElement xmlBackgroundColor = new XElement("BackgroundColor");
            XElement xmlFontColor = new XElement("FontColor");
            XElement xmlFontSize = new XElement("FontSize");
            XElement xmlFontFamily = new XElement("FontFamily");
            XElement xmlFontStyle = new XElement("FontStyle");
            XElement xmlVerticalAlignment = new XElement("VerticalAlignment");
            XElement xmlHorizontalAlignment = new XElement("TextAlignment");
            XElement xmlBorderStyle = new XElement("BorderStyle");

            xmlBackgroundColor.Value = SerializeCast.ColorToString(BackgroundColor);
            xmlFontColor.Value = SerializeCast.ColorToString(FontColor);
            xmlFontSize.Value = FontSize.ToString();
            xmlFontFamily.Value = FontFamily;
            xmlFontStyle.SetAttributeValue("IsBold",IsBold.ToString());
            xmlFontStyle.SetAttributeValue("IsItalic", IsItalic.ToString());
            xmlFontStyle.SetAttributeValue("IsUnderline", IsUnderline.ToString());
            xmlFontStyle.SetAttributeValue("IsStrikeout", IsStrikeout.ToString());


            xmlVerticalAlignment.Value = this.VerticalAlignment.ToString();
            xmlHorizontalAlignment.Value = this.TextAlignment.ToString();

            styleUnit.Add(xmlBackgroundColor);
            styleUnit.Add(xmlFontColor);
            styleUnit.Add(xmlFontSize);
            styleUnit.Add(xmlFontFamily);
            styleUnit.Add(xmlFontStyle);
            styleUnit.Add(xmlVerticalAlignment);
            styleUnit.Add(xmlHorizontalAlignment);
            styleUnit.Add(xmlBorderStyle);

            return styleUnit;

        }

        public void Resize(double columnWidth, double rowHeight)
        {
            this.Width = columnWidth;
            this.Height = rowHeight;
        }

        public BooTableStyleUnit Clone()
        {
            BooTableStyleUnit unit = (BooTableStyleUnit)this.MemberwiseClone();
            return unit;
        }
    }
}
